package com.gskj.eduservice.controller.front;

import com.alibaba.excel.event.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gskj.commonutils.JwtUtils;
import com.gskj.commonutils.ResultJson;
import com.gskj.commonutils.orderVO.CourseWebVOOrder;
import com.gskj.eduservice.client.OrderClient;
import com.gskj.eduservice.entity.EduCourse;
import com.gskj.eduservice.entity.chapter.ChapterVO;
import com.gskj.eduservice.entity.frontVO.CourseFrontVO;
import com.gskj.eduservice.entity.frontVO.CourseWebVO;
import com.gskj.eduservice.service.IEduChapterService;
import com.gskj.eduservice.service.IEduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :CodingFarmer_码农  Connor(康纳)_郭
 * @date : 19:17 20.5.22
 * @Company : http://www.gskj.com
 * @Version : 1.0
 *
 * 前台课程空控制器
 */

@RestController
@CrossOrigin
@RequestMapping("/eduservice/course/front")
public class CourseFrontController {

    @Autowired
    private IEduCourseService eduCourseService;

    @Autowired
    private IEduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    /***
     *  条件+分页查询课程集合
     * @return
     */
    @PostMapping("/get/course/list/page/{page}/{size}")
    public ResultJson getCourseList(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            // 查询条件
            @RequestBody(required = false) CourseFrontVO courseFrontVO) {

        // 分页对象
        Page<EduCourse> eduCoursePage = new Page<>(page, size);
        // 调用 service 根据条件查询分页
        Map<String, Object> map = eduCourseService.getFrontCourseListCondition(eduCoursePage, courseFrontVO);

        return ResultJson.success().data(map);
    }


    /**
     * 根据 传入的 课程id 查询课程的所有详情信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/get/front/courseinfo/{cid}")
    public ResultJson getFrontCourseInfoById(@PathVariable("cid") String courseId, HttpServletRequest request) {

        System.out.println("courseId  " + courseId);
        // 根据课程id 查询课程基本信息
        CourseWebVO courseWebVO = eduCourseService.getBaseCourseInfoById(courseId);

        // 根据课程id 查询所有的 章节、小结
        List<ChapterVO> chapterVOList = chapterService.getChapterVideoByCourseId(courseId);

        // 调用远程订单接口 ，查询当前会员浏览的课程是否已经为 会员购买
        // 从当前 请求头中获取 tolen，解析会员id
        // 会员id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println(memberId);
        // 是否已购买 标识
        Boolean aBoolean = orderClient.checkStatsByMidAndOid(memberId, courseId);

        // TODO 判断当前课程是否已经为购买
        if (aBoolean) {
            //TODO 已经购买成功，修改当前课程的销售数量+1

        }

        Map<String, Object> map = new HashMap<>();
        map.put("courseWebVO", courseWebVO);
        map.put("chapterVOList", chapterVOList);
        map.put("isbuy", aBoolean);

        return ResultJson.success().data(map);
    }

    /***
     *  根据 id  返回 课程信息， 为order 模块 调用
     *
     * @param id
     * @return
     */
    @GetMapping("/get/courseinfo/order/{id}")
    public CourseWebVOOrder getCousrseInfoOrder(@PathVariable("id") String id) {
        //1. 根据id 获取 课程信息
        CourseWebVO baseCourseInfoById = eduCourseService.getBaseCourseInfoById(id);

        // 2. 使用Beanutils 赋值
        CourseWebVOOrder courseWebVOOrder = new CourseWebVOOrder();

        //TODO  copyProperties(复制对象，被赋值对象)
        BeanUtils.copyProperties(baseCourseInfoById, courseWebVOOrder);
        return courseWebVOOrder;
    }
}
